<?php
# @Author: yongleicheng <jetox>
# @Date:   2017-11-21T02:58:02+08:00
# @Email:  admin@weiqisheng.cn
# @Project: WQSCMS
# @Filename: Weapp.php
# @Last modified by:   jetox
# @Last modified time: 2017-11-21T07:31:27+08:00
# @Copyright: 微企胜网络科技有限公司




namespace app\admin\controller;

use think\Db;
use clt\Leftnav;

class Weapp extends Common
{
    public function setting()
    {
        $table = db('system');
        $system = $table->field('id,name,url,title,key,des,bah,copyright,ads,tel,email,logo')->find($sys_id);
        $this->assign('system', $system);
        return $this->fetch();
    }
    public function nav()
    {
        if (request()->isPost()) {
            $key=input('post.key');
            $page =input('page')?input('page'):1;
            $pageSize =input('limit')?input('limit'):config('pageSize');
            $list=db('wp_nav')
                ->where('name', 'like', "%".$key."%")
                ->order('sort desc')
                ->paginate(array('list_rows'=>$pageSize,'page'=>$page))
                ->toArray();
            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
        }
        return $this->fetch();
    }
    public function add_nav($id='')
    {
        $table = db('wp_nav');
        if (request()->isPost()) {
            $datas = input('post.');
            if ($id=='') {
                if ($table->insertGetId($datas)) {
                    return json(['code' => 1, 'msg' => '导航新增成功!', 'url' => url('Weapp/nav')]);
                } else {
                    return json(array('code' => 0, 'msg' =>'新增失败！'));
                }
            } else {
                if ($table->where('id', $id)->update($datas)!==false) {
                    return json(['code' => 1, 'msg' => '编辑成功!', 'url' => url('Weapp/nav')]);
                } else {
                    return json(array('code' => 0, 'msg' =>'编辑失败!'));
                }
            }
        } else {
            return view();
        }
    }
    public function edit_nav($id='')
    {
        $table = db('wp_nav');
        if (request()->isPost()) {
            $user = db('users');
            $data = input('post.');
            $level =explode(':', $data['level']);
            $data['level'] = $level[1];
            $province =explode(':', $data['province']);
            $data['province'] = $province[1];
            $city =explode(':', $data['city']);
            $data['city'] = $city[1];
            $district =explode(':', $data['district']);
            $data['district'] = $district[1];
            if (empty($data['password'])) {
                unset($data['password']);
            } else {
                $data['password'] = md5($data['password']);
            }
            if ($user->update($data)!==false) {
                $result['msg'] = '会员修改成功!';
                $result['url'] = url('index');
                $result['code'] = 1;
            } else {
                $result['msg'] = '会员修改失败!';
                $result['code'] = 0;
            }
            return $result;
        } else {
            $result = $table->where('id', $id)->find();
            $this->assign('field', $result);
            return $this->fetch('add_nav');
        }
    }
}
